﻿using System;
using System.Net;
using System.Windows.Input;

namespace ArithCollect {
    public class Command : ICommand {
        public event Func<object, bool> CanExecuteContent;
        public bool CanExecute(object parameter) {
            if (this.CanExecuteContent != null) {
                return CanExecuteContent.Invoke(parameter);
            }
            return true;
        }
        public event EventHandler CanExecuteChanged;
        Action<object> executeAct;
        public void Execute(object parameter) {
            if (this.executeAct != null)
                this.executeAct.Invoke(parameter);
        }
        public Command(Action<object> ec) {
            this.executeAct = ec;
        }
        public void RemoveActLink() {
            executeAct = null;
        }
        public static implicit operator Action<object>(Command cmd) {
            return cmd.executeAct;
        }
        /// <summary>
        ///     class MyClass {
        ///    void M() {
        ///        var c = new Action(M2);
        ///        var c2 = c;
        ///        var c3 = Tople.C<Command, string>(c, "xx");
        ///        var c4 = Tople.C<Command, string>(c2, "xx");
        ///        M3(c3.Item1);
        ///        M3(c);
        ///        M3((Command)this.M2);
        ///    }
        ///    void M2() { }
        ///    void M3(Command c) { }
        ///}
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static implicit operator Command(Action<object> e) {
            return new Command(e);
        }
        public static implicit operator Command(Action e) {
            Action<object> ex = x => {
                e.Invoke();
            };
            return new Command(ex);
        }
        public static implicit operator Command(EventHandler eh) {
            Action<object> ex = x => {
                eh.Invoke(null, null);
            };
            return new Command(ex);
        }
    }
}
